#include <boost/test/unit_test.hpp>

#include <vector>
#include "../src/Variation.h"
#include "../src/VariationIndex.h"

using namespace std;

BOOST_AUTO_TEST_CASE( VariationIndexTest )
{
	vector<Variation> variation_list;
	variation_list.push_back(Variation("chr1", 10001,   120, -1.0, Variation::INSERTION)); // 0
	variation_list.push_back(Variation("chr1",   100,   300, -1.0, Variation::DELETION));  // 1
	variation_list.push_back(Variation("chr1",   150,   350, -1.0, Variation::DELETION));  // 2
	variation_list.push_back(Variation("chr1",   330,  1000, -1.0, Variation::INSERTION)); // 3
	variation_list.push_back(Variation("chr1",  5000,   100, -1.0, Variation::INSERTION)); // 4
	variation_list.push_back(Variation("chr1",  5000,   120, -1.0, Variation::INSERTION)); // 5
	variation_list.push_back(Variation("chr1", 10000, 10300, -1.0, Variation::DELETION));  // 6
	variation_list.push_back(Variation("chr1", 10001, 10300, -1.0, Variation::DELETION));  // 7
	variation_list.push_back(Variation("chr2", 90001, 99900, -1.0, Variation::DELETION));  // 8
	variation_list.push_back(Variation("chr2",   150,   200, -1.0, Variation::DELETION));  // 9
	variation_list.push_back(Variation("chr1",  7777,  8888, -1.0, Variation::DELETION));  // 10

	VariationIndex variation_index(variation_list);

	auto_ptr<vector<size_t> > query1 = variation_index.containedIn("chr1", 100, 300);
	BOOST_CHECK(query1.get() != 0);
	BOOST_CHECK_EQUAL(query1->size(), 1);
	BOOST_CHECK_EQUAL(query1->at(0), 1);

	auto_ptr<vector<size_t> > query2 = variation_index.containedIn("chr1", 100, 350);
	BOOST_CHECK(query2.get() != 0);
	sort(query2->begin(), query2->end());
	BOOST_CHECK_EQUAL(query2->size(), 3);
	BOOST_CHECK_EQUAL(query2->at(0), 1);
	BOOST_CHECK_EQUAL(query2->at(1), 2);
	BOOST_CHECK_EQUAL(query2->at(2), 3);

	auto_ptr<vector<size_t> > query3 = variation_index.containedIn("chr2", 80000, 100000);
	BOOST_CHECK(query3.get() != 0);
	BOOST_CHECK_EQUAL(query3->size(), 1);
	BOOST_CHECK_EQUAL(query3->at(0), 8);

	auto_ptr<vector<size_t> > query4 = variation_index.containedIn("chr1", 10000, 10300);
	BOOST_CHECK(query4.get() != 0);
	sort(query4->begin(), query4->end());
	BOOST_CHECK_EQUAL(query4->size(), 3);
	BOOST_CHECK_EQUAL(query4->at(0), 0);
	BOOST_CHECK_EQUAL(query4->at(1), 6);
	BOOST_CHECK_EQUAL(query4->at(2), 7);

	auto_ptr<vector<size_t> > query5 = variation_index.containedIn("chr1", 10001, 10300);
	BOOST_CHECK(query5.get() != 0);
	sort(query5->begin(), query5->end());
	BOOST_CHECK_EQUAL(query5->size(), 2);
	BOOST_CHECK_EQUAL(query5->at(0), 0);
	BOOST_CHECK_EQUAL(query5->at(1), 7);

	auto_ptr<vector<size_t> > query6 = variation_index.containedIn("chr1", 4900, 5099);
	BOOST_CHECK(query6.get() != 0);
	sort(query6->begin(), query6->end());
	BOOST_CHECK_EQUAL(query6->size(), 2);
	BOOST_CHECK_EQUAL(query6->at(0), 4);
	BOOST_CHECK_EQUAL(query6->at(1), 5);

	auto_ptr<vector<size_t> > query7 = variation_index.containedIn("chr2", 4900, 5099);
	BOOST_CHECK(query7.get() == 0);

	auto_ptr<vector<size_t> > query8 = variation_index.containedIn("chr2", 150000, 200000);
	BOOST_CHECK(query8.get() == 0);

	auto_ptr<vector<size_t> > query9 = variation_index.containedIn("chrX", 150000, 200000);
	BOOST_CHECK(query9.get() == 0);
}
